package com.hmall.item.web;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constants.ItemMqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import com.hmall.item.utils.SnowflakeIdWorker;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RefreshScope
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/list", name = "分页查询商品")
    public PageDTO<Item> queryByPage(@RequestParam("page") Integer page,
                                     @RequestParam("size") Integer size) {
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        Page<Item> itemPage = itemService.page(new Page<>(page, size), null);

        List<Item> items = itemPage.getRecords();
        itemPageDTO.setList(items);
        itemPageDTO.setTotal(itemPage.getTotal());
        return itemPageDTO;
    }

    @GetMapping(value = "/{id}", name = "根据id查询商品")
    public Item queryItemById(@PathVariable("id") Long id) {
        return itemService.getById(id);
    }

    @PostMapping(value = "", name = "新增商品")
    public ResponseEntity saveItem(@RequestBody Item item) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        item.setId(idWorker.nextId());
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        item.setStatus(1);
        itemService.save(item);

        // 发送新增MQ
        rabbitTemplate.convertAndSend(ItemMqConstants.EXCHANGE_NAME,
                ItemMqConstants.INSERT_KEY, item.getId());
        return ResponseEntity.ok(null);
    }

    @PutMapping(value = "/status/{id}/{status}", name = "商品上架、或者下架")
    public ResponseEntity itemUpOrDown(@PathVariable("id") Long id,
                                       @PathVariable("status") Integer status) {
        Item item = new Item();
        if (id != null && status != null) {
            if (status == 1) {
                item.setId(id);
                item.setStatus(1);
                itemService.updateById(item);
            } else {
                item.setId(id);
                item.setStatus(2);
                itemService.updateById(item);
            }
        }
        return ResponseEntity.ok(null);
    }

    @PutMapping(name = "修改商品")
    @Transactional
    public ResponseEntity updateItem(@RequestBody Item item) {
        itemService.updateById(item);
        System.out.println("商品id ====》 " + item.getId());
        // 判断是否是更新库存 还是同步  库存属性不为空-> 为订单修改
        if(item.getStock() != null){
            return ResponseEntity.ok(null);
        }
        // 发送新增MQ
        rabbitTemplate.convertAndSend(ItemMqConstants.EXCHANGE_NAME,
                ItemMqConstants.INSERT_KEY, item.getId());
        return ResponseEntity.ok(null);
    }

    @DeleteMapping(value = "/{id}", name = "删除商品")
    public ResponseEntity deleteItemById(@PathVariable("id") Long id) {
        itemService.removeById(id);
        // 发送删除MQ
        rabbitTemplate.convertAndSend(ItemMqConstants.EXCHANGE_NAME,
                ItemMqConstants.DELETE_KEY, id);
        return ResponseEntity.ok(null);
    }



}
